package com.exmaple.baidumv.thread;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.exmaple.baidumv.comm.CacheHelper;
import com.yuelan.codelib.utils.FileUtil;
import com.yuelan.codelib.utils.HttpConnent;
import com.yuelan.codelib.utils.MyHashMap;
import com.yuelan.codelib.utils.TextUtil;

/**
 * 作者: peijiangping<BR>
 * 2013-9-13下午2:02:52<BR>
 * V3BaseThread.java<BR>
 * 功能:所有的请求线程父类
 */
public class BaseHttpThread implements Runnable {
	protected Map<String, String> maps;
	protected Handler handler;
	public static final int PostSuccess = 1;
	public static final int PostFail = -1;
	public static final int ParseFail = -2;
	protected Message msg = new Message();
	protected String value = null;
	protected String url;
	public static final long SYNC_SLEEP_TIME = 10;

	public BaseHttpThread(Handler handler, Map<String, String> maps) {
		this.handler = handler;
		if (maps == null) {
			maps = new MyHashMap<String, String>();
		}
		this.maps = maps;
	}

	public BaseHttpThread(Context c, Handler handler, Map<String, String> maps) {
		this.handler = handler;
		if (maps == null) {
			maps = new MyHashMap<String, String>();
		}
		this.maps = maps;
	}

	public BaseHttpThread(String url, Handler handler, Map<String, String> maps) {
		this.url = url;
		this.handler = handler;
		if (maps == null) {
			maps = new MyHashMap<String, String>();
		}
		this.maps = maps;
	}

	public BaseHttpThread(Handler handler, Map<String, String> maps, Object obj) {
		this.handler = handler;
		if (maps == null) {
			maps = new MyHashMap<String, String>();
		}
		this.maps = maps;
	}

	@Override
	public void run() {

	}

	protected String doBaseHttpPost(String url) {
		value = HttpConnent.doHttpPost(url, getPostParams(maps), 0);
		if (handler != null) {
			if (value == null) {
				handler.sendEmptyMessage(PostFail);
			} else {
				msg.what = PostSuccess;
				msg.obj = value;
				handler.sendMessage(msg);
			}
		}
		return value;
	}

	public static String getFullUrl(String url, Map<String, String> params) {
		if (url.endsWith("?") == false) {
			url = url + "?";
		}
		StringBuilder sb = new StringBuilder(url);
		String urlStr = null;
		if (params != null) {
			for (Map.Entry<String, String> entry : params.entrySet()) {
				String key = entry.getKey();
				String value = entry.getValue();
				if (value == null) {
					value = "";
				}
				sb.append(key + "=" + value + "&");
			}
		}
		urlStr = sb.toString();
		return urlStr;
	}

	public static List<NameValuePair> getPostParams(Map<String, String> params) {
		List<NameValuePair> newparams = new ArrayList<NameValuePair>();
		if (params != null) {
			for (Map.Entry<String, String> entry : params.entrySet()) {
				String key = entry.getKey();
				String value = entry.getValue();
				if (value == null) {
					value = "";
				}
				newparams.add(new BasicNameValuePair(key, value));
			}
		}
		return newparams;
	}

	protected void doCachePost(Context c, String url, String filename) {
		File cacheFile = new File(c.getFilesDir(), filename);
		int type = CacheHelper.caCheAvailability(cacheFile, c, 0, 0);
		String json = null;
		switch (type) {
		case 1:
			json = FileUtil.readDateFile(filename, c);
			break;
		case 2:
			json = HttpConnent.doHttpPost(url, getPostParams(maps), 0);
			if (TextUtil.notNull(json)) {
				try {
					FileUtil.writeDateFile("ranklist.info",
							json.getBytes("utf-8"), c);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
			break;
		case -1:
			break;
		default:
			break;
		}
		if (type == 1 || type == 2) {
			if (TextUtil.notNull(json)) {
				Message msg = new Message();
				msg.what = 1;
				msg.obj = json;
				handler.sendMessage(msg);
			} else {
				handler.sendEmptyMessage(-1);
			}
		} else {
			handler.sendEmptyMessage(-1);
		}
	}
}
